package com.fyfeng.testgridview;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    private static final String tag = MainActivity.class.getSimpleName();

    private MyAdapter mMyAdapter;
    private GridView mGridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMyAdapter = new MyAdapter(this.getApplicationContext());
        mGridView = (GridView) findViewById(R.id.gridview);
        mGridView.setAdapter(mMyAdapter);

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Log.d(tag, "position = " + position);
                Toast.makeText(getApplicationContext(), "position = " + position, Toast.LENGTH_SHORT).show();
            }
        });

        updateView();

    }


    private void updateView() {

        String[] array = Utils.listAssets(getApplicationContext());
        mMyAdapter.items.clear();

        for (String name : array) {
            Log.d(tag, "fileName = "+name);
            if(TextUtils.isEmpty(name)){
                continue;
            }
            MyAdapter.Item item = new MyAdapter.Item();
            item.filename = name;

            mMyAdapter.items.add(item);
        }

        mMyAdapter.notifyDataSetChanged();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
